package com.bgs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bgs.common.R;
import com.bgs.pojo.Menus;
import com.bgs.mapper.MenusMapper;
import com.bgs.pojo.RolesMenus;
import com.bgs.pojo.User;
import com.bgs.pojo.vo.FourMenus;
import com.bgs.pojo.vo.MenuVo;
import com.bgs.service.MenusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bgs.service.RolesMenusService;
import com.bgs.service.UserService;
import com.bgs.utils.TokenHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 导航表 服务实现类
 * </p>
 *
 * @author Asia
 * @since 2021-09-14
 */
@Service

public class MenusServiceImpl extends ServiceImpl<MenusMapper, Menus> implements MenusService {
    @Autowired
    private TokenHelper tokenHelper;
    @Resource
    private MenusMapper menusMapper;
    @Autowired
    private RolesMenusService rolesMenusService;

    @Autowired
    private UserService userService;

    @Override
    public R queryAuthByUserRole(HttpServletRequest request) {

        String userId = tokenHelper.convertToken(request);

        if (!StringUtils.isEmpty(userId)) {

            User user = userService.getById(userId);

            if (null != user && null != user.getRoleId()) {

                QueryWrapper<RolesMenus> rolesMenusQueryWrapper = new QueryWrapper<>();
                rolesMenusQueryWrapper.eq("role_id", user.getRoleId());

                List<RolesMenus> rolesMenusList = rolesMenusService.list(rolesMenusQueryWrapper);

                List<Integer> menuIds = rolesMenusList
                        .stream()
                        .map(x -> x.getMenuId()).collect(Collectors.toList());

                List<Menus> menusList = baseMapper.selectBatchIds(menuIds);

                ArrayList<MenuVo> data = new ArrayList<>();

                ArrayList<MenuVo> res = new ArrayList<>();

                menusList.forEach(s -> {

                    MenuVo vo = new MenuVo();
                    BeanUtils.copyProperties(s, vo);

                    if (s.getHideen().equals(1)) vo.setHideen(true);
                    if (s.getHideen().equals(2)) vo.setHideen(false);

                    data.add(vo);
                });

                data.forEach(r -> {
                    if (r.getPid().equals(0)) res.add(r);
                });

                res.forEach(r -> {
                    r.setChildren(recursion(r.getMenuId(), data));
                });

                return R.ok().data("data", res);

            } else {
                return R.error().message("没有此用户");
            }
        } else {
            return R.error().message("没有此用户");
        }
    }

    @Override
    public R findMenu() {
        List<Menus> list=menusMapper.findMenu();
        return R.ok().data("data",list);
    }
    //查询权限
    @Override
    public R fourQueryMenu() {

        List<FourMenus> menu = menusMapper.findMenu1();
        List<MenuVo> mList = new ArrayList<>();
        for (FourMenus fourMenus : menu) {
            MenuVo m1 = new MenuVo();
            m1.setMenuId(fourMenus.getMenuId());
            m1.setTitle(fourMenus.getTitle());
            mList.add(m1);

            for (FourMenus menuVo : fourMenus.getChildren()) {
                MenuVo m2 = new MenuVo();
                m2.setMenuId(menuVo.getMenuId());
                m2.setTitle(menuVo.getTitle());
                m1.getChildren().add(m2);
            }
        }

        return R.ok().data("data",mList);
    }

    /**
     * 修改权限
     *
     * @param map
     * @return
     */
    @Override
    public R fourEditMenu(Map<String, Object> map) {
        Integer roleId = Integer.parseInt(map.get("roleId").toString());
        String permIds = map.get("permIds").toString();
        String[] split = permIds.substring(1, permIds.length() - 1).split(",");
        List<RolesMenus> list = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            RolesMenus rolesMenus = new RolesMenus();
            String trim = split[i].trim();
            rolesMenus.setRoleId(roleId);
            rolesMenus.setMenuId(Integer.parseInt(trim));
            list.add(rolesMenus);
        }
        Integer i = menusMapper.fourEditMenu(roleId,list);
        return R.ok().data("data",i);
    }

    public List<MenuVo> recursion(Integer id, List<MenuVo> data) {

        ArrayList<MenuVo> res = new ArrayList<>();

        data.forEach(r -> {
            if (r.getPid().equals(id)) {
                res.add(r);
            }
        });

        res.forEach(r -> {
            r.setChildren(recursion(r.getMenuId(), data));
        });

        if (res.size() > 0) {
            return res;
        }

        return null;
    }
}
